﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace FilterIllegalCharacter.UI
{
    /// <summary>
    /// 字典文件帮助类
    /// </summary>
    public class DictFileHelper
    {
        /// <summary>
        /// 读取所有字典文件
        /// </summary>
        /// <returns></returns>
        public static string[] ReadAllDictFile()
        {
            var list1 = ReadRemoteDictFile();
            var list2 = ReadLocalDictFile();
            return list1.Concat(list2).Distinct().ToArray();
        }
        /// <summary>
        /// 读取远程字典文件
        /// </summary>
        /// <returns></returns>
        public static List<string> ReadRemoteDictFile()
        {
            return ReadFile("dict/remote.txt");
        }
        /// <summary>
        /// 写入远程字典文件
        /// </summary>
        /// <param name="content"></param>
        public static void WriteRemoteDictFile(List<string> content)
        {
            OverwriteFile("dict/remote.txt", content);
        }
        /// <summary>
        /// 读取本地字典文件
        /// </summary>
        /// <returns></returns>
        public static List<string> ReadLocalDictFile()
        {
            return ReadFile("dict/local.txt");
        }
        /// <summary>
        /// 写入本地字典文件
        /// </summary>
        /// <returns></returns>
        public static void WriteLocalDictFile(List<string> content)
        {
            OverwriteFile("dict/local.txt", content);
        }
        //private static string GetAbsolutePath(string relativePath)
        //{
        //    // 获取当前执行程序集所在的目录
        //    string assemblyLocation = Assembly.GetExecutingAssembly().Location;
        //    string directoryName = Path.GetDirectoryName(assemblyLocation);

        //    // 使用Path.Combine来拼接路径
        //    string absolutePath = Path.Combine(directoryName, relativePath);
        //    return absolutePath;
        //}
        /// <summary>
        /// 覆盖写入文件内容
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        private static void OverwriteFile(string path, List<string> content)
        {
            // 确保文件存在，如果不存在，则创建文件
            File.WriteAllText(path, string.Join("\r\n", content));
        }
        /// <summary>
        /// 读取文件内容
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> ReadFile(string path)
        {
            List<string> result;
            // 确保文件存在
            if (File.Exists(path))
            {
                result = File.ReadAllLines(path).ToList(); // 读取文件所有内容
            }
            else
            {
                result = new List<string>();
            }
            return result;
        }
    }
}
